<?php

namespace app\admin\model;

use think\Model;

class Backup extends Model
{
    protected $insert = ['md5' => '', 'special' => 0, 'desc' => ''];
    protected $updateTime = false;
    protected $connection = [
        'type' => 'sqlite',
        'database' => '../backup.db',
        'prefix' => 'ycyp_',
        'auto_timestamp' => 'int',
        'debug' => true,
    ];
    public function check()
    {
        $file = $this->file_path;
        $md5 = md5_file($file);

        return $md5 == $this->md5;
    }
    public function getFilePathAttr($value, $data)
    {
        return ROOT_PATH.'public'.DS.'sql'.DS.$data['filename'];
    }
    public function setMd5Attr($value, $data)
    {
        if (empty($value)) {
            $file = $this->getFilePathAttr($value, $data);
            $md5 = md5_file($file);

            return $md5;
        }

        return $value;
    }
    public function delete()
    {
        unlink($this->file_path);

        return parent::delete();
    }
}
